<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聊天室 - 登录</title>
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <div class="auth-container">
        <div class="auth-card animated">
            <div class="auth-header">
                <h2>欢迎回来</h2>
            </div>
            <div class="auth-body">
                <div id="error-message" class="error-message" style="color: red; margin-bottom: 15px; display: none;"></div>
                <form id="login-form">
                    <div class="form-group">
                        <input type="text" id="username" name="username" class="form-control" placeholder="用户名" required>
                    </div>
                    <div class="form-group">
                        <input type="password" id="password" name="password" class="form-control" placeholder="密码" required>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn" style="width: 100%;">登录</button>
                    </div>
                </form>
            </div>
            <div class="auth-footer">
                <p>还没有账号？ <a href="/register" class="auth-switch">立即注册</a></p>
            </div>
        </div>
    </div>

    <script>
        // 清除之前可能存在的用户数据
        localStorage.removeItem('user');
        
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            fetch('/api/users/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    return response.json().then(data => {
                        throw new Error(data.error || '登录失败');
                    });
                }
            })
            .then(data => {
                console.log('登录成功，用户数据:', data);
                
                // 确保用户对象是完整的
                if (!data || !data.id || !data.username) {
                    console.error('服务器返回的用户数据不完整:', data);
                    throw new Error('登录成功但用户数据不完整');
                }
                
                // 先清除本地存储中可能存在的旧数据
                localStorage.removeItem('user');
                
                // 保存用户信息到本地存储
                localStorage.setItem('user', JSON.stringify(data));
                console.log('保存到localStorage的用户数据:', data);
                console.log('用户ID:', data.id);
                console.log('用户名:', data.username);
                
                // 确认localStorage中的数据是否正确保存
                const savedData = JSON.parse(localStorage.getItem('user'));
                console.log('从localStorage读取的用户数据:', savedData);
                
                // 跳转到用户列表页面
                window.location.href = '/users';
            })
            .catch(error => {
                const errorMessage = document.getElementById('error-message');
                errorMessage.textContent = error.message;
                errorMessage.style.display = 'block';
            });
        });
    </script>
</body>
</html> 